package org.jeecg.modules.simulator.model.train.manager.trainmanager;

import org.jeecg.modules.simulator.model.MetroTrain;
import org.jeecg.modules.simulator.model.train.action.TrainActionRecorder;
import org.jeecg.modules.simulator.model.train.event.TrainDownLoadEvent;
import org.jeecg.modules.simulator.model.train.event.msg.Train2LastStationMsg;
import org.jeecg.modules.simulator.rooter.TrainManager;
import org.jeecg.modules.simulator.util.SpringUtil;

import java.time.LocalTime;
import java.util.Optional;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2024-10-04 13:25
 **/
public class EndState  implements TrainState{
    @Override
    public void handle(TrainManager trainManager,
                       String key, MetroTrain train, String now) {
        Optional<TrainActionRecorder> result = trainManager.getTrainActionRdsManager().getTrainActionRecordersMap().get(key).getTrainActionRecordMap().values().stream()
                .filter(TrainActionRecorder::isLast)
                .findFirst();

        LocalTime localTime = LocalTime.parse(now);

        if (result.isPresent()) {
            String stationName = result.get().getName();
            Train2LastStationMsg train2LastStationMsg = new Train2LastStationMsg();
            trainManager.downLoadNet(train);
            train2LastStationMsg.setTrain(train);
            train2LastStationMsg.setStopStationName(stationName);
            train2LastStationMsg.setTime(localTime);
            SpringUtil.getApplicationContext().publishEvent(new TrainDownLoadEvent(train2LastStationMsg));
        }
    }
}
